package org.sixtynine.stock.controller.front;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.sixtynine.stock.Constants.ChartType;
import org.sixtynine.stock.criteria.StockSearchCriteria;
import org.sixtynine.stock.model.ColData;
import org.sixtynine.stock.model.ColModel;
import org.sixtynine.stock.model.GridData;
import org.sixtynine.stock.service.StockManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilanka
 * 
 */
@Controller
public class StockOverViewController extends AbstractGridController {

	@Autowired
	private StockManagerService service;

	@RequestMapping(value = "/overview/main")
	public ModelAndView showResults() {
		ModelAndView modelAndView = new ModelAndView("/overview/main");
		showBannar(modelAndView);
		return modelAndView;
	}

	@RequestMapping(value = "/overview/list", method = RequestMethod.POST)
	@ResponseBody
	public GridData getData(
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "rows", required = false, defaultValue = "20") int rows,
			@RequestParam(value = "sidx", required = false) String sidx,
			@RequestParam(value = "sord", required = false) String sord,
			@RequestParam(value = "search", required = false) String search,
			@ModelAttribute StockSearchCriteria criteria,
			HttpServletRequest request) {
		
		
		criteria.setChartType(ChartType.GENERAL);
		return getGridData(criteria, page, rows, sidx, sord, search, request);

	}

	@RequestMapping(value = "/overview/colData", method = RequestMethod.GET)
	@ResponseBody
	public ColData getColumnData() {
		return getColData();
	}

	@Override
	public ColData getColData() {
		ColData colData = new ColData();

		List<ColModel> colModels = new ArrayList<ColModel>();

		colModels.add(new ColModel("ticker"));
		colModels.add(new ColModel("companyName"));
		// ading alignment to test and number format
		colModels.add(new ColModel("currentSharePrice", "number", "right"));
		colModels.add(new ColModel("marketCap", "integer", "right"));
		colModels.add(new ColModel("per", "number", "right"));
		colModels.add(new ColModel("pbv", "number", "right"));
		colModels.add(new ColModel("dy", "number", "right"));
		colModels.add(new ColModel("oneWeekPricePer", "number", "right"));
		colModels.add(new ColModel("oneMonthPricePer", "number", "right"));
		colModels.add(new ColModel("twelMonthPricePer", "number", "right"));
		colModels.add(new ColModel("ytdPricePer", "number", "right"));
		colModels.add(new ColModel("twelMonthHigh", "number", "right"));
		colModels.add(new ColModel("twelMonthLow", "number", "right"));
		colModels.add(new ColModel("change", "number", "right"));
		colModels.add(new ColModel("volume", "integer", "right"));
		colData.setColModel(colModels);

		List<String> colNames = new ArrayList<String>();

		colNames.add("Ticker");
		colNames.add("Company");
		colNames.add("Share Price");
		colNames.add("Mkt Cap");
		colNames.add("PER");
		colNames.add("PBV");
		colNames.add("DY%");
		colNames.add("1W");
		colNames.add("1M");
		colNames.add("12M");
		colNames.add("YTD");
		colNames.add("12 High");
		colNames.add("12 Low");
		colNames.add("Change");
		colNames.add("Volume");
		colData.setColNames(colNames);
		return colData;
	}

}
